<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :span="24" :xs="24">
        <el-main>
            <el-card class="box-card" style="margin-bottom:20px">
                <el-col>
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="small"
              @click="handleAdd"
              >新增用户</el-button>
        </el-col>
                <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          label-width="80px"
          style="text-align: center"
        >
          <el-form-item label="查询用户" prop="user_id">
            <el-input
              v-model="queryParams.user_id"
              placeholder="请输入用户ID"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
            <el-input type="text" style="display:none" />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
            </el-card>

          <el-card class="box-card">
            <el-row>
            <el-col>
            <div class="text-item">
              <el-table v-loading="loading" :data="userList">
                <el-table-column label="用户ID" align="center" prop="user_id" />
                <el-table-column label="用户名称" align="center" prop="user_name" />
                <el-table-column label="用户角色" align="center" prop="role_name" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="140">
                  <template slot-scope="scope">
                    <el-button
                      size="small"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row)"
                      >修改</el-button>
                    <el-button
                      size="small"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                      >删除</el-button>
                 </template>
                </el-table-column>
              </el-table>
            </div>
            </el-col>
            </el-row>
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNum"
                :page-sizes="[5, 10, 50, 100]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                style="text-align: center;margin-top:15px"
                >
            </el-pagination>
          </el-card>
        </el-main>
      </el-col>
    </el-row>

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="user_id">
          <el-input :disabled="true" v-model="form.user_id"/>
        </el-form-item>
        <el-form-item label="用户名称" prop="user_name">
          <el-input v-model="form.user_name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="用户角色" prop="role_id">
          <el-select v-model="form.role_id" clearable placeholder="请选择">
            <el-option v-for="item in userRoleOptions" :key="item.userRoleId" :label="item.userRoleName" :value="item.userRoleId" @keyup.enter.native="handleQuery"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
  .clearfix {
    overflow: auto;
  }
  .el-header{
    background-color: #ffffff;
    color: #333;
    width: 100%;
    text-align: left;
    line-height: 50px;
  }

</style>

<script>
import { listUsers,getUserById,updateUser,deleteUser, addUser} from "@/api/users";
export default {
  name: "Users",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //用户角色
      userRoleOptions:[
        {
            userRoleId:1,
            userRoleName:'管理员'
        },
        {
            userRoleId:2,
            userRoleName:'店长'
        },
        {
            userRoleId:3,
            userRoleName:'销售员'
        },
      ],
      // 总条数
      total: 0,
      // 当前页
      pageNum: 0,
      // 每页数据量
      pageSize: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        user_id: undefined,
        user_name: undefined,
        role_id: undefined,
        role_name:undefined,
      },
      // 表单参数
      form: {},
      rules: {
        userName: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listUsers(this.queryParams).then((response) => {
        this.userList = response.data.list;
        this.total = response.data.total;
        this.pageNum = response.data.pageNum;
        this.pageSize = response.data.pageSize;
        this.loading = false;
      });
    },
    /** 处理分页 */
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    },

    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.resetForm();
    },

    /** 表单重置 */
    resetForm() {
      this.form = {
        user_id: undefined,
        user_name: undefined,
        role_id: undefined,
        role_name:undefined,
      };
    },

    /** 搜索按钮操作 */
    handleQuery() {
        if(this.queryParams.user_id){
            getUserById(this.queryParams.user_id).then((response) => {
                this.userList = response.data;
                if(this.userList[0]){
                  this.$message.success("查询成功");
                }else{
                  this.$message.error("输入的用户ID无效");
                }
                this.resetQueryParams();
            });
        }else{
            this.getList()
            this.$message.error("请输入用户ID");
        }
      
    },
    resetQueryParams() {
      this.queryParams = {
        user_name: undefined,
        user_id: undefined,
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        user_name: undefined,
        user_id: undefined,
      };
      this.getList();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm();
      this.open = true;
      this.title = "新增用户";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resetForm();
      getUserById(row.user_id).then((response) => {
        this.form = response.data[0];
        this.open = true;
        this.title = "修改用户信息";
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.title == "修改用户信息") {
            updateUser(this.form).then((response) => {
              this.open = false;
              this.title = "";
              this.getList();
              this.$message.success("修改用户信息成功"); //后端返回消息字符串
            });
          } else {
            addUser(this.form).then((response) => {
              this.open = false;
              this.title = "";
              this.getList();
              this.$message.success("添加用户成功");
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        '是否确认删除用户"' + row.user_name + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return deleteUser(row.user_id);
        })
        .then(() => {
          this.getList();
          this.$message.success("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>